using System;
using System.Collections.Generic;
using System.Text;

namespace IGGCore
{
    /// <summary>
    /// IGGCore main channel.
    /// It is responsible of loading online information and delivering it
    /// to consumer applications.
    /// </summary>
    public class MainChannel
    {
        private Scoreboard m_scoreBoard;
        private ScoreboardParser m_scoreboardParser;
        private LineupParser m_lineupParser;
        private StatisticsParser m_statsParser;
        private NewsParser m_newsParser;
        private HistoryParser m_historyParser;
        private BoxscoreParser m_boxscoreParser;
        private StandingsParser m_standingsParser;
        private PlayersParser m_playersParser;
        private TwitterParser m_twitterParser;
        private TransactionParser m_transactionParser;
        private DepthChartParser m_depthChartParser;
        private BaseballReferenceParser m_referenceParser;
        private InjuriesParser m_injuriesParser;

        /// <summary>
        /// The current newsparser
        /// Used to register to its parsing event
        /// </summary>
        public NewsParser MainNewsParser
        {
            get
            {
                return m_newsParser;
            }
        }

        /// <summary>
        /// The unique scoreboard
        /// </summary>
        public Scoreboard ScoreBoard
        {
            get { return m_scoreBoard; }
        }

        /// <summary>
        /// The unique scoreboard
        /// </summary>
        public Scoreboard ScoreBoardToday
        {
            get { return m_scoreBoard; }
        }

        /// <summary>
        /// The unique scoreboard
        /// </summary>
        public Scoreboard ScoreBoardDated
        {
            get;
            set;
        }

        public MainChannel()
        {
        }

        public void Initialize()
        {
            Indicators.Initialize();
            m_scoreBoard = new Scoreboard();
            m_lineupParser = new LineupParser();
            m_newsParser = new NewsParser();
            m_statsParser = new StatisticsParser();
            m_historyParser = new HistoryParser();
            m_boxscoreParser = new BoxscoreParser();
            m_standingsParser = new StandingsParser();
            m_playersParser = new PlayersParser();
            m_twitterParser = new TwitterParser();
            m_transactionParser = new TransactionParser();
            m_scoreboardParser = new ScoreboardParser(m_playersParser);
            m_depthChartParser = new DepthChartParser();
            m_referenceParser = new BaseballReferenceParser();
            m_injuriesParser = new InjuriesParser();
        }

        /// <summary>
        /// Update the scoreboard statistics
        /// </summary>
        public void UpdateStatistics()
        {
            m_statsParser.ParseStatistics(m_scoreBoard);
        }

        public void UpdateDepthChart()
        {
            TeamCruncher.CrunchTeam(m_scoreBoard);
            m_scoreBoard.ActiveTeam.DepthChart = m_depthChartParser.GetDepthChart(m_scoreBoard.ActiveTeam.DepthChartSource);
        }

        public void UpdateBaseballReference()
        {
            m_referenceParser.ParseReference();
        }

        public void UpdateInjuries()
        {
            m_injuriesParser.ParseInjuries(m_scoreBoard);
        }

        public void SetBaseballReferenceTidbit()
        {
            m_referenceParser.SetRankingTidbit(m_scoreBoard);
        }

        /// <summary>
        /// Update the scoreboard active team
        /// </summary>
        public void UpdateTeam()
        {
            TeamCruncher.CrunchTeam(m_scoreBoard);
            m_scoreBoard.ActiveTeam.News = m_newsParser.GetRssNews(m_scoreBoard.ActiveTeam.RssFeed, 9, false);
            m_scoreBoard.ActiveTeam.BeatTweets = m_twitterParser.GetBeatTweets(m_scoreBoard.ActiveTeam.BeatWriters, 6);
            m_scoreBoard.ActiveTeam.DepthChart = m_depthChartParser.GetDepthChart(m_scoreBoard.ActiveTeam.DepthChartSource);
            // Get Standings Info
            // Get Last Game Info
            // Get Next Game
        }

        public void UpdateActiveTeamNews()
        {
            m_scoreBoard.ActiveTeam.News = m_newsParser.GetRssNews(m_scoreBoard.ActiveTeam.RssFeed, 9, false);
        }

        /// <summary>
        /// Update the scoreboard standings
        /// </summary>
        public void UpdateStandings()
        {
            m_standingsParser.ParseStandings(m_scoreBoard);
            m_standingsParser.ParseWildCardStandings(m_scoreBoard);
        }

        /// <summary>
        /// Update the transactions
        /// </summary>
        public void UpdateTransactions()
        {
            List<TransactionDay> days = m_transactionParser.GetTransactions();
            m_scoreBoard.Transactions = days;
        }

        /// <summary>
        /// Update the scoreboard history tidbit
        /// </summary>
        public void LoadHistory()
        {
            m_historyParser.LoadHistory();
        }

        public string GetHistoryTidbit()
        {
            return m_historyParser.GetHistoryTidbit();
        }

        /// <summary>
        /// Update the selected game boxscore
        /// </summary>
        public void UpdateBoxscore(DateTime currentDate)
        {
            m_boxscoreParser.ParseBoxscore(currentDate, m_scoreBoard.SelectedGame);
        }

        /// <summary>
        /// Update the selected game boxscore
        /// </summary>
        public void UpdateAllBoxscores(DateTime currentDate, Scoreboard board)
        {
            foreach (Game game in board.GamesList.Games)
            {
                m_boxscoreParser.ParseBoxscore(currentDate, game);
            }
        }

        /// <summary>
        /// Update the selected game boxscore
        /// </summary>
        public void UpdateBoxscore(DateTime currentDate, Game game)
        {
            if (game != null)
            {
                m_boxscoreParser.ParseBoxscore(currentDate, game);
            }
        }

        /// <summary>
        /// Update the selected game offensive and defensive alignment
        /// </summary>
        public void UpdateTeamAlignment(DateTime currentDate)
        {
            m_playersParser.ParsePlayers(currentDate, m_scoreBoard.SelectedGame);
            m_lineupParser.ParseLineup(currentDate, m_scoreBoard.SelectedGame);
        }

        /// <summary>
        /// Get a new single news for the scoreboard
        /// </summary>
        public void UpdateNews(bool picNeeded)
        {
            m_newsParser.UpdateNews(m_scoreBoard, picNeeded);
        }

        /// <summary>
        /// Get the latest tweets for the scoreboard
        /// </summary>
        public void UpdateTweets()
        {
            m_scoreBoard.Tweets = m_twitterParser.GetTouzelTweets(8); 
        }

        /// <summary>
        /// Return the latest notification for the specified game
        /// </summary>
        public string GetNotification(DateTime currentDate, Game game)
        {
            string result = null;
            m_scoreboardParser.ParseNotifications(currentDate, game);
            return result;
        }

        /// <summary>
        /// Refresh the scoreboard
        /// </summary>
        public void RefreshScoreboard(DateTime currentDate, string selectedGameID)
        {
            Scoreboard olBoard = m_scoreBoard;
            try
            {
                m_scoreBoard = m_scoreboardParser.ParseScoreboardFromWebSources(currentDate, selectedGameID);
                m_scoreBoard.Date = ToolBox.GetScoreboardDate();
                m_scoreBoard.News = olBoard.News;
                m_scoreBoard.Tweets = olBoard.Tweets;
                m_scoreBoard.LastUpdate = DateTime.Now;
            }
            catch (Exception ex)
            {
                m_scoreBoard = olBoard;
                throw new Exception("Unable to load scoreboard - " + ex.Message, ex);
            }
        }

        /// <summary>
        /// Refresh the scoreboard
        /// </summary>
        public void RefreshScoreboardDated(DateTime date)
        {
            Scoreboard olBoard = ScoreBoardDated;
            try
            {
                ScoreBoardDated = m_scoreboardParser.ParseScoreboardFromWebSources(date, null);
                ScoreBoardDated.Date = date;
                m_scoreBoard.News = olBoard.News;
                m_scoreBoard.LastUpdate = DateTime.Now;
            }
            catch (Exception ex)
            {
                ScoreBoardDated = olBoard;
                throw new Exception("Unable to load scoreboard dated - " + ex.Message, ex);
            }
        }

    }
}
